package org.jeecg.modules.xdbuse.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.xdbuse.entity.FxjBuse0310;
import org.jeecg.modules.xdbuse.entity.FxjBuse0320;
import org.jeecg.modules.xdbuse.entity.FxjBuse03;
import org.jeecg.modules.xdbuse.vo.FxjBuse03Page;
import org.jeecg.modules.xdbuse.service.IFxjBuse03Service;
import org.jeecg.modules.xdbuse.service.IFxjBuse0310Service;
import org.jeecg.modules.xdbuse.service.IFxjBuse0320Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 放款订单详情
 * @Author: zhaodui
 * @Date:   2020-02-20
 * @Version: V1.0
 */
@RestController
@RequestMapping("/xdbuse/fxjBuse03")
@Slf4j
@Api(tags="放款订单详情")
public class FxjBuse03Controller {
	@Autowired
	private IFxjBuse03Service fxjBuse03Service;
	@Autowired
	private IFxjBuse0310Service fxjBuse0310Service;
	@Autowired
	private IFxjBuse0320Service fxjBuse0320Service;
	
	/**
	 * 分页列表查询
	 *
	 * @param fxjBuse03
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	@ApiOperation("放款订单详情列表")
	public Result<?> queryPageList(FxjBuse03 fxjBuse03,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FxjBuse03> queryWrapper = QueryGenerator.initQueryWrapper(fxjBuse03, req.getParameterMap());
		Page<FxjBuse03> page = new Page<FxjBuse03>(pageNo, pageSize);
		IPage<FxjBuse03> pageList = fxjBuse03Service.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param fxjBuse03Page
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FxjBuse03Page fxjBuse03Page) {
		FxjBuse03 fxjBuse03 = new FxjBuse03();
		BeanUtils.copyProperties(fxjBuse03Page, fxjBuse03);
		fxjBuse03Service.saveMain(fxjBuse03, fxjBuse03Page.getFxjBuse0310List(),fxjBuse03Page.getFxjBuse0320List());
		return Result.ok("添加成功！");
	}
	 /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain")
	 public Result<?> addmain(@RequestBody FxjBuse03 fxjBuse03) {
  		 fxjBuse03Service.save(fxjBuse03);
		 return Result.ok(fxjBuse03);
	 }


	 /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain10")
	 public Result<?> addmain10(@RequestBody FxjBuse0310 fxjBuse0310) {
		 fxjBuse0310Service.save(fxjBuse0310);
		 return Result.ok(fxjBuse0310);
	 }
	 /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain20")
	 public Result<?> addmain20(@RequestBody FxjBuse0320 fxjBuse0320) {
		 fxjBuse0320Service.save(fxjBuse0320);
		 return Result.ok(fxjBuse0320);
	 }



	/**
	 *  编辑
	 *
	 * @param fxjBuse03Page
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FxjBuse03Page fxjBuse03Page) {
		FxjBuse03 fxjBuse03 = new FxjBuse03();
		BeanUtils.copyProperties(fxjBuse03Page, fxjBuse03);
		FxjBuse03 fxjBuse03Entity = fxjBuse03Service.getById(fxjBuse03.getId());
		if(fxjBuse03Entity==null) {
			return Result.error("未找到对应数据");
		}
		fxjBuse03Service.updateMain(fxjBuse03, fxjBuse03Page.getFxjBuse0310List(),fxjBuse03Page.getFxjBuse0320List());
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		fxjBuse03Service.delMain(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.fxjBuse03Service.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		FxjBuse03 fxjBuse03 = fxjBuse03Service.getById(id);
		if(fxjBuse03==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(fxjBuse03);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryFxjBuse0310ByMainId")
	public Result<?> queryFxjBuse0310ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FxjBuse0310> fxjBuse0310List = fxjBuse0310Service.selectByMainId(id);
		return Result.ok(fxjBuse0310List);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryFxjBuse0320ByMainId")
	public Result<?> queryFxjBuse0320ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FxjBuse0320> fxjBuse0320List = fxjBuse0320Service.selectByMainId(id);
		return Result.ok(fxjBuse0320List);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param fxjBuse03
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FxjBuse03 fxjBuse03) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<FxjBuse03> queryWrapper = QueryGenerator.initQueryWrapper(fxjBuse03, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
      List<FxjBuse03> queryList = fxjBuse03Service.list(queryWrapper);
      // 过滤选中数据
      String selections = request.getParameter("selections");
      List<FxjBuse03> fxjBuse03List = new ArrayList<FxjBuse03>();
      if(oConvertUtils.isEmpty(selections)) {
          fxjBuse03List = queryList;
      }else {
          List<String> selectionList = Arrays.asList(selections.split(","));
          fxjBuse03List = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
      }

      // Step.3 组装pageList
      List<FxjBuse03Page> pageList = new ArrayList<FxjBuse03Page>();
      for (FxjBuse03 main : fxjBuse03List) {
          FxjBuse03Page vo = new FxjBuse03Page();
          BeanUtils.copyProperties(main, vo);
          List<FxjBuse0310> fxjBuse0310List = fxjBuse0310Service.selectByMainId(main.getId());
          vo.setFxjBuse0310List(fxjBuse0310List);
          List<FxjBuse0320> fxjBuse0320List = fxjBuse0320Service.selectByMainId(main.getId());
          vo.setFxjBuse0320List(fxjBuse0320List);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "放款订单详情列表");
      mv.addObject(NormalExcelConstants.CLASS, FxjBuse03Page.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("放款订单详情数据", "导出人:"+sysUser.getRealname(), "放款订单详情"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<FxjBuse03Page> list = ExcelImportUtil.importExcel(file.getInputStream(), FxjBuse03Page.class, params);
              for (FxjBuse03Page page : list) {
                  FxjBuse03 po = new FxjBuse03();
                  BeanUtils.copyProperties(page, po);
                  fxjBuse03Service.saveMain(po, page.getFxjBuse0310List(),page.getFxjBuse0320List());
              }
              return Result.ok("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.ok("文件导入失败！");
    }

}
